/**
 * Copyright 2009 University of Washington This code is released under the GPL version 3 - with a copy of the license
 * included
 * 
 * @author Maya Hao Li
 */
package seedpod.webapp.view.htmlwidget;

import java.util.Vector;

public class Table extends GenericHtmlWidget {
	private int					_cellspacing			= 0;
	private int					_cellpadding			= 0;
	private boolean				_alternateRowColor		= true;
	private String				_evenRowColor			= "#ededed";
	private String				_oddRowCssClass			= "";
	private String				_evenRowCssClass		= "";
	private int					_border					= 0;
	private int					_currRowIndex			= -1;
	protected int				_maxCellsPerRow			= 0;
	private Vector<TableRow>	_rows;
	private String				_onMouseOverCssClass	= "";

	public Table() {
		_rows = new Vector<TableRow>();
	}

	public Table(String widgetID) {
		super( widgetID );
		_rows = new Vector<TableRow>();
	}

	public TableCell addCell( Object cellContent ) {
		return addCellComponent( _currRowIndex, cellContent.toString() );
	}

	public TableCell addCell( String cellContent ) {
		return addCellComponent( _currRowIndex, cellContent );
	}

	public TableCell addCellComponent( int rowIndex, String cellContent ) {
		TableRow row = _rows.get( rowIndex );
		TableCell cell = row.addCell( cellContent );
		setMaxCellsPerRow( row );
		return cell;
	}

	public TableRow addRow() {
		TableRow tr = new TableRow( this, _currRowIndex );
		_rows.add( tr );
		_currRowIndex++;
		return tr;
	}

	public int getBorder() {
		return _border;
	}

	public int getCurrRowIndex() {
		return _currRowIndex;
	}

	/**
	 * must implemented function that's called by the render (writer) function
	 * 
	 * @return
	 */
	@Override
	public String render() {
		StringBuffer table = new StringBuffer();
		table.append( "<table " );
		table.append( ATT( "cellspacing", _cellspacing ) );
		table.append( ATT( "cellpadding", _cellpadding ) );
		table.append( ATT( "border", _border ) );
		table.append( ATT( "class", _cssClass ) );
		table.append( ATT( "id", _widgetID ) );
		table.append( ">" );

		for ( int r = 0; r < _rows.size(); r++ ) {
			TableRow row = _rows.get( r );

			if ( _alternateRowColor && !row.isHeader() ) if ( (r + 1) % 2 == 0 ) {
				if ( row.getRowColor().length() == 0 ) row.setRowColor( _evenRowColor );
				row.setCssClass( _evenRowCssClass );
			} else row.setCssClass( _oddRowCssClass );

			if ( _onMouseOverCssClass.length() > 0 ) row.setOnMouseOverCssClass( _onMouseOverCssClass );

			table.append( row.render() );
		}
		table.append( "</table>" );
		return table.toString();
	}

	public void setAlternativeRowColor( boolean alt ) {
		_alternateRowColor = alt;
	}

	public void setAlternativeRowCssClass( String odd, String even ) {
		_oddRowCssClass = odd;
		_evenRowCssClass = even;
	}

	public void setBorder( int b ) {
		_border = b;
	}

	private void setMaxCellsPerRow( TableRow row ) {
		if ( _maxCellsPerRow < row._numCells ) _maxCellsPerRow = row._numCells;
	}

	public void setOnMouseOverClass( String className ) {
		_onMouseOverCssClass = className;
	}

	public void setTableCellSpacing( int cellspacing, int cellpadding ) {
		_cellspacing = cellspacing;
		_cellpadding = cellpadding;
	}
}
